package com.abigdreamer.im.test.ui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;

public class MdiApplicationDemo extends JFrame implements ActionListener {
	JButton b = new JButton("创建内部窗体   ");
	JDesktopPane desktopPane = new JDesktopPane();
	int windowCount = 1;

	public MdiApplicationDemo() {
		super.setTitle("internalFrame!");
		// 获取内容窗格容器
		Container contentPane = getContentPane();
		// 创建功能按钮面板
		JPanel pnlControlPane = new JPanel();
		pnlControlPane.add(b);
		// 设置桌面窗体的布局管理为FlowLayout，缺省为null
		desktopPane.setLayout(new FlowLayout());

		// 添加事件监听器
		b.addActionListener(this);

		// 把功能按钮面板和桌面窗体加入到内容窗格容器中
		contentPane.add(pnlControlPane, BorderLayout.NORTH);
		contentPane.add(desktopPane, BorderLayout.CENTER);
	}

	// 程序的入口方法
	public static void main(String[] args) {
		MdiApplicationDemo frame = new MdiApplicationDemo();
		// 设置框架窗体的事件监听(关闭窗体事件)
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		// 显示框架窗体
		frame.setSize(600, 400);
		frame.setVisible(true);
	}

	// 处理创建内部窗体按钮事件
	public void actionPerformed(ActionEvent event) {
		System.out.println("new   InternalFrame!");
		// 创建一个新的内部窗体
		JInternalFrame jif = new JInternalFrame("内部窗体[" + windowCount++ + "]", // title
				true, // resizable
				true, // closable
				true, // maximizable
				true); // iconifiable
		// 设置内部窗体大小为250×100
		jif.setPreferredSize(new Dimension(desktopPane.getSize()));
		jif.getContentPane().add(new JButton("test"));
		jif.show();
		// 把内部窗体加入到桌面窗体中
		desktopPane.add(jif);
		// 刷新
		desktopPane.revalidate();
	}
}
